﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace FYPDemo
{
    public partial class Advertisement_Maintenance3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindAdvertisementPeriod();
                bindAdvertisementPictureSize();
            }
        }

        protected void bindAdvertisementPeriod()
        {
            DataTable dt = new DataTable();
            dt = BackOffice_Maintenance.listAdvertisementPeriod();

            rblPeriod.DataSource = dt;
            rblPeriod.DataTextField = "Period";
            rblPeriod.DataValueField = "ID";
            rblPeriod.DataBind();
        }

        protected void bindAdvertisementPictureSize()
        {
            DataTable dt = new DataTable();
            dt = AdvertisementManagement.listAdvertisementPictureSize();

            rblPicSize.DataSource = dt;
            rblPicSize.DataTextField = "PictureSize";
            rblPicSize.DataValueField = "ID";
            rblPicSize.DataBind();
        }
        
        protected void btnAdd2_Click(object sender, EventArgs e)
        {
            int result = 0;
            int period = Convert.ToInt32(rblPeriod.SelectedValue);
            int picSize = Convert.ToInt32(rblPicSize.SelectedValue);
            string position = ddlPosition.SelectedValue;
            string img = ImgUpload.ImageUrl.ToString();

            if (img != "")
            {
                System.IO.File.Delete(Server.MapPath(img));
                HttpPostedFile theFile = Session["PostedFile"] as HttpPostedFile;
                uploadFile(theFile, "Advertisement", position);
                ImgUpload.ImageUrl = "~/Advertisement/" + position + ".jpg";
                img = ImgUpload.ImageUrl.ToString();
            }

            result = BackOffice_Maintenance.addUniversaleAdv(img, picSize, period, position);

            if (result > 0)
            {
                MessageBox.Show("Successfully add a new advertisement.");
            }
        }

        private void uploadFile(HttpPostedFile theFile, string uploadTo, string filename)
        {
            if (theFile != null && theFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath("~/" + uploadTo)))
                    Directory.CreateDirectory(Server.MapPath("~/" + uploadTo));

                string fname = filename + Path.GetExtension(Path.GetFileName(theFile.FileName));
                theFile.SaveAs(Server.MapPath(Path.Combine("~/" + uploadTo + "/", fname)));
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string adv = "TempAdvertisement";
            string uploadTo = "Advertisement";

            if (FileUpload1.HasFile)
            {
                if (Directory.Exists(Server.MapPath("~/" + uploadTo + "/" + adv)))
                {
                    System.IO.File.Delete(Server.MapPath("~/" + uploadTo + "/" + adv));
                }

                else
                {
                    HttpPostedFile theFile = FileUpload1.PostedFile;
                    Session["PostedFile"] = theFile;
                    uploadFile(theFile, uploadTo, adv);
                    ImgUpload.ImageUrl = "~/" + uploadTo + "/" + adv + ".jpg";
                }
            } 
        }
    }
}